package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.CmsCategory;
import com.example.demo.mapper.CmsCategoryMapper;

@Service
public class CmsCategoryService {
	@Autowired
	private CmsCategoryMapper categoryMapper;
	
	public List<CmsCategory> allCategoryByPid(){
		List<CmsCategory> s = categoryMapper.allCategoryWithParent();
		if(s != null) {
			return s;
		}
		return new ArrayList<CmsCategory>();
	}
	
	public void insert(CmsCategory category) {
		if(category.getId()!=null) {
			categoryMapper.insert(category);
			System.out.println("有id");
		}else {
			String id = UUID.randomUUID().toString();
			category.setId(id);
			categoryMapper.insert(category);
			System.out.println("无id");
		}
	}
	
	public int deleteById(String id) {
		return categoryMapper.deleteByPrimaryKey(id);
	}
	
	public void saveOrUpdate(CmsCategory category) {
		if(category.getId()!=null) {
			categoryMapper.updateByPrimaryKey(category);
			System.out.println("有id");
		}else {
			String id = UUID.randomUUID().toString();
			category.setId(id);
			categoryMapper.updateByPrimaryKey(category);
			System.out.println("无id");
		}
	}
	
	public List<CmsCategory> findAll(){
		return categoryMapper.selectAll();
	}
	
	public CmsCategory selectById(String id) {
		return categoryMapper.selectByPrimaryKey(id);
	}
}
